package offer;

/**
 * @Author Elephas
 * @Date 2022/1/22
 **/
public class MinDepth {
    public int minDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        return minDepthHelper(root,0);
    }
    public int minDepthHelper(TreeNode root,int depth){
        if(root.left != null && root.right != null){
            return Math.min(minDepthHelper(root.left,depth+1),minDepthHelper(root.right,depth+1));
        }else if(root.left != null){
            return minDepthHelper(root.left,depth+1);
        }else if(root.right != null){
            return minDepthHelper(root.right,depth+1);
        }else{
            return depth;
        }
    }
}
